package com.cndatacom.teleweb.dao.impl;

import com.cndatacom.teleweb.common.PaginationModel;
import com.cndatacom.teleweb.common.PaginationSupport;
import com.cndatacom.teleweb.dao.IWorkNewsDao;
import com.cndatacom.teleweb.model.WorkNews;

public class WorkNewsDao extends PaginationSupport implements IWorkNewsDao {

	public WorkNews addWorkNews(WorkNews workNews) {
		getHibernateTemplate().save(workNews);
		return workNews;
	}

	public boolean delWorkNews(WorkNews workNews) {
		getHibernateTemplate().delete(workNews);
		return true;
	}

	public PaginationModel getAllWorkNews(int offset, int pageSize) {
		return searchPaginated("select wn from com.cndatacom.teleweb.model.WorkNews wn order by wn.publishDate desc", offset, pageSize);
	}

	public PaginationModel getPublishWorkNews(int offset, int pageSize) {
		return searchPaginated("select wn from com.cndatacom.teleweb.model.WorkNews wn where publishDate <= current_timestamp() order by wn.publishDate desc", offset, pageSize);
	}
	
	public WorkNews getWorkNewsById(int workId) {
		return (WorkNews)getHibernateTemplate().get(WorkNews.class, workId);
	}

	public WorkNews updateWorkNews(WorkNews workNews) {
		getHibernateTemplate().update(workNews);
		return workNews;
	}

	public void addClickCount(WorkNews workNews) {
		if(workNews != null){
			workNews.setClickCount( workNews.getClickCount() + 1);
			updateWorkNews(workNews);
		}
	}
}
